package demo.admin.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import demo.admin.common.BaseResult;
import demo.admin.common.ResultCode;
import demo.admin.config.GlobalConfig;
import demo.admin.model.vo.UserLoginRequestVO;
import demo.admin.model.vo.UserLoginResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(tags = "user")
@Slf4j
@RestController
@RequestMapping("user")
public class ApiUserController {

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "登录接口")
    @PostMapping("login")
    public BaseResult login(@RequestBody UserLoginRequestVO userLoginRequestVO)  {
        if("admin".equals(userLoginRequestVO.getUsername()) && "admin".equals(userLoginRequestVO.getPassword())){
            //JWT的token
            String token = JWT.create()
                    .setPayload("uid", "admin")
                    //过期时间设置
                    .setExpiresAt(DateUtil.offset(DateUtil.date(), DateField.MINUTE, GlobalConfig.EXPIRE_TIME))
                    .setKey(GlobalConfig.SECRET_KEY.getBytes())
                    .sign();
            UserLoginResponseVO userLoginResponseVO = new UserLoginResponseVO("admin","admin","admin","1", new String[]{"*.*.*"}, token);
            return BaseResult.success(userLoginResponseVO);
        }else {
            return BaseResult.error(ResultCode.FAILED_INVALID_PWD);
        }
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "注销接口")
    @GetMapping("loginOut")
    public BaseResult loginOut()  {
        return BaseResult.success(null);
    }
}
